home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / modula2 / mod2src.arc / REAL2MON.MOD < prev    next >
Text File  |  1987-03-25  |  3KB  |  97 lines

  1. IMPLEMENTATION MODULE Real2Mon;
  2.  
  3. (*            Copyright (c) 1987 - Coronado Enterprises            *)
  4.  
  5. FROM InOut IMPORT Write;
  6.  
  7. VAR OutString : ARRAY[0..80] OF CHAR;
  8.  
  9. (* This procedure uses a rather lengthy method for decomposing the *)
  10. (* REAL number and forming it into single characters.  There is a  *)
  11. (* procedure available in the Logitech library to do this for you  *)
  12. (* but this method is kept as an example of how to decompose the   *)
  13. (* number to prepare it for output.  It could be much more effi-   *)
  14. (* cient to use the Logitech library call. The Procedure is named  *)
  15. (* RealConversions.RealTOString, see your library reference.       *)
  16.  
  17. PROCEDURE WriteReal(DataOut  : REAL;
  18.                     FieldSize : CARDINAL;
  19.                     Digits    : CARDINAL);
  20.  
  21. VAR Index          : CARDINAL;
  22.     Field          : CARDINAL;
  23.     Count          : CARDINAL;
  24.     WholeFieldSize : CARDINAL;
  25.     ABSDataOut     : REAL;
  26.     Char           : CHAR;
  27.     RoundReal      : REAL;
  28.  
  29. BEGIN
  30.    IF DataOut >= 0.0 THEN   (* Get the absolute value to work with *)
  31.       ABSDataOut := DataOut;
  32.    ELSE
  33.       ABSDataOut := -DataOut;
  34.    END;
  35.  
  36.                          (* Make sure the Digits field is positive *)
  37.    IF Digits < 0 THEN
  38.       Digits := 0;
  39.    END;
  40.  
  41.         (* Make sure there are 3 or more digits for the whole part *)
  42.    IF (FieldSize - Digits) < 3 THEN
  43.       FieldSize := Digits + 3;
  44.    END;
  45.  
  46.    RoundReal := 0.5;         (* This is used for rounding the data *)
  47.    IF Digits = 0 THEN
  48.       WholeFieldSize := FieldSize;
  49.    ELSE
  50.       WholeFieldSize := FieldSize - Digits - 1;
  51.       FOR Count := 1 TO Digits DO
  52.          RoundReal := RoundReal * 0.1;    (* Reduce for each digit *)
  53.       END;
  54.    END;
  55.    ABSDataOut := ABSDataOut + RoundReal;    (* Add rounding amount *)
  56.  
  57.    Count := 0;
  58.    WHILE ABSDataOut >= 1.0 DO
  59.       Count := Count + 1;              (* Count significant digits *)
  60.       ABSDataOut := 0.1 * ABSDataOut;
  61.    END;
  62.  
  63.    WHILE WholeFieldSize > (Count + 1) DO  (* Output leading blanks *)
  64.       Write(" ");
  65.       WholeFieldSize := WholeFieldSize - 1;
  66.    END;
  67.  
  68.    IF DataOut >= 0.0 THEN          (* Output the sign (- or blank) *)
  69.       Write(" ");
  70.    ELSE
  71.       Write("-");
  72.    END;
  73.  
  74.    WHILE Count > 0 DO       (* Output the whole part of the number *)
  75.       ABSDataOut := 10.0 * ABSDataOut;
  76.       Index := TRUNC(ABSDataOut);
  77.       Char := CHR(Index + 48);                   (* 48 = ASCII '0' *)
  78.       Write(Char);
  79.       ABSDataOut := ABSDataOut - FLOAT(Index);
  80.       Count := Count - 1;
  81.    END;
  82.  
  83.    IF Digits > 0 THEN  (* Output the fractional part of the number *)
  84.       Write('.');
  85.       FOR Count := 1 TO Digits DO
  86.          ABSDataOut := 10.0 * ABSDataOut;
  87.          Index := TRUNC(ABSDataOut);
  88.          Char := CHR(Index + 48);                (* 48 = ASCII '0' *)
  89.          Write(Char);
  90.          ABSDataOut := ABSDataOut - FLOAT(Index);
  91.       END;
  92.    END;
  93. END WriteReal;
  94.  
  95. END Real2Mon.
  96.  
  97.